package com.by.yz.mypersonal;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.chatuidemo.DemoHelper;
import com.by.yz.chatuidemo.ui.UserProfileActivity;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 123 on 2016/10/7.
 */
public class PersonalUpdateInfoActivity extends BaseActivity {
    private EditText intoText;
    private Button ok_bt;
    private String IntoText;
    private ProgressDialog dialog;
    UserTest userTest;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_updateinfo);
        setToolbarTitle("修改昵称");
        initView();
        initDatas();
    }

    private void initDatas() { }

    private void initView() {
        Intent intent=this.getIntent();
        final UserTest user=(UserTest)intent.getSerializableExtra("user1");
        intoText=(EditText)findViewById(R.id.edit_nick);
        intoText.setText(user.getnickName());
        ok_bt=(Button)findViewById(R.id.personal_updateinfo_bt);
        ok_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                IntoText=intoText.getText().toString();
                if(IntoText.equals("")){
                    Toast.makeText(getApplicationContext(), "输入不能为空，请重新输入", Toast.LENGTH_SHORT).show();
                }else {
                    dialog = ProgressDialog.show(PersonalUpdateInfoActivity.this, getString(R.string.dl_update_nick), getString(R.string.dl_waiting));
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            user.setnickName(IntoText);
                            JSONObject jsonObject = ToJSonObject.beanToJSONObejct(user);
                            UserRESTFul userRESTFul = new UserRESTFul();
                            userRESTFul.putUser(jsonObject, new UserRESTFul.UserPOSTCallBack() {
                                @Override
                                public void ok(JSONObject response) throws JSONException {
                                    //updateRemoteNick(IntoText);
                                    dialog.dismiss();
                                    Toast.makeText(PersonalUpdateInfoActivity.this, getString(R.string.toast_updatenick_success), Toast.LENGTH_SHORT)
                                            .show();
                                    PersonalUpdateInfoActivity.this.finish();
                                }
                                @Override
                                public void error(String response) {
                                    dialog.dismiss();
                                    Toast.makeText(getApplicationContext(), "昵称修改失败，请检查网络", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }).start();
                    dialog.show();
                }
            }
        });
    }
    private void updateRemoteNick(final String nickName) {
        dialog = ProgressDialog.show(this, getString(R.string.dl_update_nick), getString(R.string.dl_waiting));
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean updatenick = DemoHelper.getInstance().getUserProfileManager().updateCurrentUserNickName(nickName);
                if (PersonalUpdateInfoActivity.this.isFinishing()) {
                    return;
                }
                if (!updatenick) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(PersonalUpdateInfoActivity.this, getString(R.string.toast_updatenick_fail), Toast.LENGTH_SHORT)
                                    .show();
                            dialog.dismiss();
                        }
                    });
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            Toast.makeText(PersonalUpdateInfoActivity.this, getString(R.string.toast_updatenick_success), Toast.LENGTH_SHORT)
                                    .show();
                            PersonalUpdateInfoActivity.this.finish();
                        }
                    });
                }
            }
        }).start();
    }

}
